#include "./build.h"


#ifdef BUILD_APP

#include "./task/app_task.h"

using namespace task;

int main() {
    osKernelInitialize();
    appTask.start();
    osKernelStart();
}

#endif

#ifdef BUILD_ROOT

#include "./cfg/ota_info.h"
#include "./device/compat.h"

using namespace cfg;
using namespace driver;
using namespace utils;

using AppFunc = void (*)(void);

int main() {
    Error err = otaInfo.load();
    if (err || (!otaInfo->verify())) {
        goto finish;
    }

    flash::erase(flash::APP_ADDR, otaInfo->pkg_size);
    flash::write(flash::APP_ADDR,
                 reinterpret_cast<const void *>(flash::OTA_DATA_ADDR),
                 otaInfo->pkg_size);
    otaInfo.erase();
finish:
    uint32_t sp = *reinterpret_cast<const volatile uint32_t *>(flash::APP_ADDR);
    uint32_t pc =
        *reinterpret_cast<const volatile uint32_t *>(flash::APP_ADDR + 4);
    AppFunc app = reinterpret_cast<AppFunc>(pc);
    __set_MSP(sp);
    app();
}

#endif

